﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Data
{
    public class ProductDao : IProductDao
    {
        public int GetProductCountByFilter(string name, int productGroupId, int manufacturerId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                IEnumerable<Product> query = context.Products;

                //Filters
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (productGroupId > 0)
                {
                    query = query.Where(x => x.ProductGroupId == productGroupId);
                }

                if (manufacturerId > 0)
                {
                    query = query.Where(x => x.ManufacturerId == manufacturerId);
                }

                return query.Count();
            }
        }

        public List<Product> GetProductsByFilter(string name, int productGroupId, int manufacturerId, int startIndex, int count, string sorting)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                IEnumerable<Product> query = context.Products;

                //Filters
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (productGroupId > 0)
                {
                    query = query.Where(x => x.ProductGroupId == productGroupId);
                }
                if (manufacturerId > 0)
                {
                    query = query.Where(x => x.ManufacturerId == manufacturerId);
                }

                //Sorting
                if (string.IsNullOrEmpty(sorting) || sorting.Equals("Name ASC"))
                {
                    query = query.OrderBy(x => x.Name);
                }
                else if (sorting.Equals("Name DESC"))
                {
                    query = query.OrderByDescending(x => x.Name);
                }
                else if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(x => x.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(x => x.Code);
                }
                else
                {
                    query = query.OrderBy(c => c.Name); //Default!
                }

                return count > 0
                           ? query.Skip(startIndex).Take(count).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public Product AddProduct(Product product)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                context.Products.AddObject(product);
                context.SaveChanges();
            }

            return product;
        }

        public void UpdateProduct(Product product)
        {
            if (product == null || product.Id == 0) return;

            using (var context = new MonkeyWarrantyEntities())
            {
                context.Products.Attach(product);
                context.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
                context.SaveChanges();
            }
        }

        public void DeleteProduct(int productId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                var product = context.Products.FirstOrDefault(x => x.Id == productId);
                if (product == null) return;
                context.DeleteObject(product);
                context.SaveChanges();
            }
        }

        public int GetMaxId()
        {
            var maxId = 0;

            using (var context = new MonkeyWarrantyEntities())
            {
                var productGroup = context.Products.OrderByDescending(x => x.Id).FirstOrDefault();
                if (productGroup != null) maxId = productGroup.Id;
            }

            return maxId;
        }

        public int GetCurrentIdentity()
        {
            long? identity;
            using (var context = new MonkeyWarrantyEntities())
            {
                identity = context.GetIdentity("Products").SingleOrDefault();
            }

            return identity.HasValue ? (int)identity.Value : 0;
        }

        public Product GetById(int id)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.Products.FirstOrDefault(x => x.Id == id);
            }
        }

        public InstantTicketProductView GetProductViewById(long ticketServiceId)
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                return db.InstantTicketProductViews.FirstOrDefault(x => x.Id == ticketServiceId);
            }
        }
    }
}
